pub mod thread_pool;

extern crate mongodb;
use crate::thread_pool::ThreadPool;
use mongodb::bson::doc;
use mongodb::options::ClientOptions;
use mongodb::Client;
use std::io::prelude::*;
use std::io::BufReader;
use std::net::TcpListener;
use std::net::TcpStream;

struct WideEvent {
    /** 接收时间 */
    _time: i64,

    /** 发生时间 */
    _when: i64,

    /** “当事人” */
    _who: String,

    /** “地点” */
    _where: String,

    /** “事件” */
    _what: String,
}

fn main() {
    let pool = ThreadPool::new(16);
    let listener = TcpListener::bind("127.0.0.1:12345").unwrap();
    for stream in listener.incoming() {
        let stream = stream.unwrap();
        pool.execute(|| handle_connection(stream));
    }

    // add_event(WideEvent {
    //     _time: 0,
    //     _when: 0,
    //     _who: String::from("who"),
    //     _where: String::from("who"),
    //     _what: String::from("who"),
    // })
}

fn handle_connection(mut stream: TcpStream) {
    let buf_reader = BufReader::new(&mut stream);
    let http_request: Vec<_> = buf_reader
        .lines()
        .map(|result| result.unwrap())
        .take_while(|line| !line.is_empty())
        .collect();

    println!("Request: {:#?}", http_request);

    let response = "HTTP/1.1 200 OK\r\n\r\n";
    stream.write_all(response.as_bytes()).unwrap();
    stream.flush().unwrap();
}

// fn add_event(event: WideEvent) {}
